package com.androidx.p013lv.base.model;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.androidx.p013lv.base.Constance;
import com.androidx.p013lv.base.event.M3u8Event;
import com.androidx.p013lv.base.http.HttpUtils;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.greenrobot.eventbus.EventBus;

/* renamed from: com.androidx.lv.base.model.M3u8Model */
public class M3u8Model extends ViewModel {
    private MutableLiveData<String> data;
    private int mType;
    private int retryTimes;

    public MutableLiveData<String> getM3u8Data() {
        if (this.data == null) {
            this.data = new MutableLiveData<>();
        }
        return this.data;
    }

    public void getM3u8(final String str, int i) {
        this.retryTimes = 2;
        this.mType = i;
        final String substring = str.substring(0, str.lastIndexOf("/"));
        new Thread(new Runnable() {
            /* class com.androidx.p013lv.base.model.M3u8Model.RunnableC10281 */

            public void run() {
                try {
                    M3u8Model.this.parseIndex(str, substring);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void parseIndex(String str, String str2) throws IOException {
        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
        httpURLConnection.setConnectTimeout(5000);
        httpURLConnection.setReadTimeout(5000);
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
            File file = new File(Constance.savePath, "kdbc");
            if (!file.exists() || !file.isDirectory()) {
                file.mkdir();
            }
            File file2 = new File(Constance.m3u8SavePath);
            if (file2.exists()) {
                file2.delete();
            }
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file2, false));
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine == null) {
                    bufferedWriter.flush();
                    bufferedWriter.close();
                    bufferedReader.close();
                    EventBus.getDefault().post(new M3u8Event(Constance.m3u8SavePath, this.mType));
                    return;
                } else if (readLine.startsWith("#")) {
                    if (readLine.startsWith("#EXT-X-KEY")) {
                        readLine = "#EXT-X-KEY:METHOD=AES-128,URI=\"/android_asset/mt.key\"";
                    }
                    bufferedWriter.write(readLine);
                    bufferedWriter.newLine();
                } else {
                    bufferedWriter.write(str2 + "/" + readLine);
                    bufferedWriter.newLine();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            int i = this.retryTimes;
            if (i != 0) {
                this.retryTimes = i - 1;
                parseIndex(str, str2);
            }
        }
    }

    public void cancelHttp() {
        HttpUtils.getInsatance().cancelTag("m3u8");
        HttpUtils.getInsatance().cancelTag("Dynamicm3u8");
    }
}
